package com.net.UDP;

import javax.swing.plaf.basic.BasicTextAreaUI;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * 服务端
 *1.创建服务器+端口
 * 2、准备接受容器
 * 3、封装成包
 *4、接受数据
 * 5.分析数据
 *6、释放
 * @author jiangbang
 * @date 2016/7/22
 */
public class MyServer {
    public static void main(String[] args) throws IOException {
        //1.创建服务器+端口
        DatagramSocket server = new DatagramSocket(8888);
        //2、准备接受容器
        byte[] container = new byte[1024];
        //3、封装成包
        DatagramPacket packet = new DatagramPacket(container,container.length);
        //4、接受数据
        server.receive(packet);
        //5、分析数据
        byte[] data = packet.getData();
        int len = packet.getLength();
        System.out.println(new String(data,0,len));
        System.out.println(convert(data));
        //释放
        server.close();
    }

    /**
     * 字节数组+data输入流
      * @param data
     * @return
     */
    public static double convert(byte[] data) throws IOException {
        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
        double num = dis.readDouble();
        dis.close();
        return num;
    }
}
